

#pragma once

#include "type.h"
#include "gameobject.h"
#include "CCSkeletalSprite.h"

#include <string>
#include <list>
#include <map>

class Bullet;
class SimpleAnimation;
class GamePlayScene;
class Flag;

static const int UNIT_DEFAULT_SPEED = 50;

class Unit {
public:
	Unit();
	virtual ~Unit();

    virtual void set(const string& name, const Point& pos, GamePlayScene* scene);
	virtual void setAttribute(const GameObjectAttribute& attr){
		attribute = attr;
		hp = attribute.health;
	}
	virtual bool update(float ticks) = 0;
    virtual void transformState(UnitState newState);

    virtual void clearBullets();
	virtual void handleBulletHit(Bullet* bullet, float damageFactor = 1.0f);
   
    virtual Point getStrikePoint();
	virtual float getClosedFightRange();
	Point getEstimatedStrikePoint(float ticks);
    Point getEstimatedPosition(float ticks);
    Point getSpeed(){
		return speed;
    }

	virtual void moveTo(const Point& pos);
    
    virtual bool isUnderAttack() {
        return !bullets.empty();
    }

	virtual bool isAlive() {
		return (state != UnitState::dying && state != UnitState::died);
	}

	virtual bool isOver() {
		return (state == UnitState::died || state == UnitState::escaped);
	}
    
	bool isInsideEllipse(const Point& pos, float range);
	bool isInsideBound(const Point& pt);
    
private:
	static int seed;

public:
	string unitName;

    int id;
    int level;
    
	Unit* target;
	Flag* myFlag;
    
    UnitType unitType;
    UnitState state;
	UnitState previousState;

    GameObjectAttribute attribute;

    float hp;
    
    list<Bullet*> bullets;
    
    Point position;
    Point speedStep;
    Point speed;
    Point targetPosition;
    
	CCSkeletalSprite* sprite;
	GamePlayScene* gamePlayScene;
    
protected:
	virtual void handleEnterState();
	virtual void handleExitState();

    virtual void updateBullets(float ticks);

	virtual void onAttack(EventData* event);

	virtual bool move(float ticks);

	virtual void attack() = 0;
	virtual void shoot() = 0;
	virtual void cast() = 0;

protected:
	Sprite* _sprLifebarBg;
	ProgressTimer* _sprLifebar;

	float _ticks;

	LabelTTF* _labelDebug;

	char buf[100];
};

